package org.yy.service.impl;

import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.yy.service.MailService;


@Service
public class MailServiceImpl implements MailService {
    private final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;

    @Override
    public Boolean sendSimpleMail(String to, String subject, String content) {
        try {
            SimpleMailMessage simpleMessage = new SimpleMailMessage();
            simpleMessage.setTo(to);
            simpleMessage.setSubject(subject);
            simpleMessage.setText(content);
            simpleMessage.setFrom(from);
            mailSender.send(simpleMessage);
            logger.info("发送邮件成功！");
        } catch (Exception e) {
            logger.error("发送邮件失败！", e);
            return false;
        }

        return true;
    }
}
